package net.prasenjit.test.webmvc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.stereotype.Controller;
import org.springframework.validation.DefaultMessageCodesResolver;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Locale;

@Controller
@RequestMapping("/message")
public class MessageResolveController {

    @Autowired
    private MessageSource messageSource;

    @RequestMapping
    @ResponseBody
    public String testMessageResolver(Locale locale) {

        DefaultMessageSourceResolvable error;
        DefaultMessageCodesResolver resolver = new DefaultMessageCodesResolver();
        String[] codel = resolver.resolveMessageCodes("bvl", "price", "child[1].name", null);
        error = new DefaultMessageSourceResolvable(codel);
        return messageSource.getMessage(error, locale);
    }

    @RequestMapping(value = "/bop", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String trapBOP(@RequestBody String request) {
        System.out.println(request);
        return "got it";
    }
}
